# 完成一个从 https://developer.aliyun.com/article/1449769 网页下载所有图片的案例
import requests
from bs4 import BeautifulSoup
# bs4是BeautifulSoup库的包名（Beautiful Soup 4版本）
# #BeautifulSoup是一个用于解析HTML和XML文档的Python库，用于提取和操作网页数据(用于网络爬虫数据抓取)
import os  # 提供了与操作系统交互的功能(文件目录创建删除遍历等，路径访问，进程管理)
import urllib.parse
from urllib.parse import urljoin # urljoin用于将两个URL进行智能拼接,它会根据相对路径和绝对路径的规则，正确地组合基础URL和目标URL


def download_images_from_page(url, save_folder="downloaded_images"):
    """
    从指定网页下载所有图片

    Args:
        url (str): 目标网页URL
        save_folder (str): 保存图片的文件夹名称
    """
    # 创建保存图片的目录
    if not os.path.exists(save_folder):
        os.makedirs(save_folder)

    # 设置请求头，模拟浏览器访问
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
    }

    try:
        # 获取网页内容
        response = requests.get(url, headers=headers)
        response.raise_for_status()
        response.encoding = response.apparent_encoding

        # 解析HTML
        soup = BeautifulSoup(response.text, 'html.parser')

        # 查找所有img标签
        img_tags = soup.find_all('img')

        print(f"找到 {len(img_tags)} 个图片标签")

        # 下载每张图片
        for i, img in enumerate(img_tags):
            # 获取图片URL
            img_url = img.get('src') or img.get('data-src')

            if not img_url:
                continue

            # 处理相对路径
            img_url = urljoin(url, img_url)

            # 获取图片文件名
            parsed_url = urllib.parse.urlparse(img_url)
            img_name = os.path.basename(parsed_url.path)

            # 如果没有文件名，使用默认名称
            if not img_name or '.' not in img_name:
                img_name = f"image_{i}.jpg"

            # 构造完整的保存路径
            save_path = os.path.join(save_folder, img_name)

            # 下载图片
            try:
                img_response = requests.get(img_url, headers=headers)
                img_response.raise_for_status()

                with open(save_path, 'wb') as f:
                    f.write(img_response.content)

                print(f"已下载: {img_name}")

            except Exception as e:
                print(f"下载图片失败 {img_url}: {e}")

    except Exception as e:
        print(f"获取网页失败: {e}")


# 使用示例
if __name__ == "__main__":  # if __name__ == "__main__": - 判断当前模块是否作为主程序运行
    target_url = "https://www.zhangxinxu.com/wordpress/2024/07/html%e5%b9%b6%e4%b8%8d%e7%ae%80%e5%8d%95/"
    download_images_from_page(target_url)
